package com.gongpingjialoan.activity.loans;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.bean.LoanFinishEB;
import com.gongpingjialoan.bean.LoanStatusData;
import com.gongpingjialoan.bean.VipBuycarBean;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.LoanUtil;
import com.gongpingjialoan.view.LoanCarModelView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 完成审核
 */
public class CompleteVerificationActivity extends BaseActivity implements View.OnClickListener {
    private TextView errow_msgT, again;
    private TextView current_priceT, documents;
    private LoanCarModelView car_mode_layout;
    String status = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete_verification);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        errow_msgT = (TextView) findViewById(R.id.errow_msg);
        again = (TextView) findViewById(R.id.again);
        current_priceT = (TextView) findViewById(R.id.current_price);
        documents = (TextView) findViewById(R.id.documents);

        again.setOnClickListener(this);
        documents.setOnClickListener(this);

        String push = getIntent().getStringExtra("push");
        if (TextUtils.isEmpty(push)) {
            getLoanStatus();
        } else {
            status = getIntent().getStringExtra("status");
            setViewInfo(status);
        }

        car_mode_layout = (LoanCarModelView) findViewById(R.id.car_mode_layout);
    }

    public void setViewInfo(String status) {
        if (LoanUtil.STATUS_PHOTO_FAILED.equals(status) || LoanUtil.STATUS_PHOTO_FAILED_2.equals(status)) {
            setTitle("审核失败");
            findViewById(R.id.defeated_layout).setVisibility(View.VISIBLE);
            findViewById(R.id.succeed_layout).setVisibility(View.GONE);
            String msg = LoanStatusData.getInstance().getPhoto_msg();
            errow_msgT.setText(TextUtils.isEmpty(msg) ? "照片不清楚" : msg);
            if (LoanUtil.STATUS_PHOTO_FAILED_2.equals(status)) {
                again.setVisibility(View.GONE);
            }

            PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanRefuseView);


        } else if (LoanUtil.STATUS_PHOTO_SUCCESS.equals(status)) {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanApproveView);
            setTitle("完成审核");
            findViewById(R.id.defeated_layout).setVisibility(View.GONE);
            findViewById(R.id.succeed_layout).setVisibility(View.VISIBLE);
            java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
            current_priceT.setText(df.format(Float.valueOf(LoanStatusData.getInstance().getCredit_limit())) + "元");
        }
    }

    private void getLoanStatus() {
        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                Toast.makeText(mySelf, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                try {
                    JSONObject jo = new JSONObject(json);
                    if (jo == null) {
                        return;
                    }
                    if ("error".equals(jo.getString("status"))) {
                        Toast.makeText(mySelf, jo.getString("msg"), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    LoanUtil.toLoanData(json);
                    setViewInfo(LoanStatusData.getInstance().getStatus());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, 1);
        netDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN + API.campaigncars_evaluations);
        netDataJson.setUseOtherDomain(true);
        netDataJson.request("get");
    }

    @Override
    public void onClick(View v) {
        Intent it;
        switch (v.getId()) {
            case R.id.again:
                PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanRefuseReload);
                it = new Intent(mySelf, LoanCertificateActivity.class);
                startActivity(it);
                break;
            case R.id.documents:
                PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanApproveCredit);
                it = new Intent(mySelf, LoanDocumentsActivity.class);
                startActivity(it);
                break;
        }
    }

    // 选择完车型后,调用这辆
    @Subscribe
    public void onEventMainThread(VipBuycarBean vipBuycarBean) {
        car_mode_layout.onBrandSelceted(vipBuycarBean);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(LoanFinishEB event) {
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        if (LoanUtil.STATUS_PHOTO_FAILED.equals(status) || LoanUtil.STATUS_PHOTO_FAILED_2.equals(status)) {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanRefuseBack);
        } else {
            PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanApproveBack);
        }
    }
}
